package com.company.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.entity.SysContentEntity;
import com.company.project.entity.SysDept;
import com.company.project.service.*;
import com.company.project.vo.req.OrganMessageQueryReqVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysOrganMessageEntity;


/**
 * 部门介绍
 *
 * @author YangYulin
 * @email YangYulinit@163.com
 * @date 2020-06-22 21:57:52
 */
@Controller
@RequestMapping("/")
@Api(tags = "系统管理-机构新闻")
@Slf4j
public class SysOrganMessageController {
    @Autowired
    private SysOrganMessageService sysOrganMessageService;
    @Autowired
    private HttpSessionService httpSessionService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private SysContentService sysContentService;


    /**
     * 跳转到页面
     */
    @GetMapping("/index/sysOrganMessage/list")
    public String sysOrganMessageList() {
        return "sysorganmessage/list";
    }

    @GetMapping("/index/sysOrganMessage")
    public String sysOrganMessage() {
        return "sysorganmessage/msg";
    }


    @ApiOperation(value = "新增")
    @PostMapping("/sysOrganMessage/add")
    @LogAnnotation(title = "机构新闻", action = "新增")
    @RequiresPermissions("sysOrganMessage:add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public DataResult add(@RequestBody OrganMessageQueryReqVO sysContent) {
        SysDept sysDept = deptService.noeDeptInfo();


        log.info(JSONObject.toJSONString(sysContent));
        // 记录文章信息
//        SysContentEntity sysContentEntity =
        sysOrganMessageService.saveNews(sysContent);
        // 关联 介绍
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/sysOrganMessage/update")
    @LogAnnotation(title = "机构新闻", action = "更新")
    @RequiresPermissions("sysOrganMessage:update")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public DataResult update(@RequestBody OrganMessageQueryReqVO sysContent) {
        SysDept sysDept = deptService.noeDeptInfo();
        log.info(JSONObject.toJSONString(sysContent));
        SysOrganMessageEntity organMessageEntity = sysOrganMessageService.getById(sysContent.getId());

        // 更新 type
        organMessageEntity.setType(Integer.valueOf(sysContent.getType()));
        sysOrganMessageService.updateById(organMessageEntity);
        // 更新 文章
        SysContentEntity instContet = new SysContentEntity();
        instContet.setId(organMessageEntity.getContentId());
        instContet.setTitle(sysContent.getTitle()); // 标题
        instContet.setContent(sysContent.getContent()); // 内容
        sysContentService.updateById(instContet);

        // 记录文章信息
//        SysContentEntity sysContentEntity = sysOrganMessageService.saveNews(sysContent);
        return DataResult.success();
    }


    @ApiOperation(value = "查询分页数据")
    @PostMapping("/sysOrganMessage/findListByPage")
    @RequiresPermissions("sysOrganMessage:list")
    @LogAnnotation(title = "机构新闻", action = "查询分页数据")
    @ResponseBody
    public DataResult findListByPage(@RequestBody OrganMessageQueryReqVO organMessageQueryReqVO) {
        if (StringUtils.isEmpty(organMessageQueryReqVO.getDeptId())) {
            SysDept sysDept = deptService.noeDeptInfo();
            log.info(JSONObject.toJSONString(sysDept));
            if (null == sysDept) {
                organMessageQueryReqVO.setDeptId("0");
            } else {
                organMessageQueryReqVO.setDeptId(sysDept.getId());
            }
        }
        return DataResult.success(sysOrganMessageService.selectMyPage(organMessageQueryReqVO));
    }

//    @ApiOperation(value = "查询分页数据")
//    @PostMapping("/sysOrganMessage/detailInfo_api")
////    @RequiresPermissions("sysOrganMessage:list")
//    @LogAnnotation(title = "机构新闻", action = "查询分页数据")
//    @ResponseBody
//    public DataResult detailInfo(@RequestBody OrganMessageQueryReqVO organMessageQueryReqVO) {
//        if (StringUtils.isEmpty(organMessageQueryReqVO.getDeptId())) {
//            SysDept sysDept = deptService.noeDeptInfo();
//            log.info(JSONObject.toJSONString(sysDept));
//            if (null == sysDept) {
//                organMessageQueryReqVO.setDeptId("0");
//            } else {
//                organMessageQueryReqVO.setDeptId(sysDept.getId());
//            }
//        }
//        return DataResult.success(sysOrganMessageService.selectMyPage(organMessageQueryReqVO));
//    }

    @GetMapping("/sysOrganMessage/{type}")
    @ApiOperation(value = "查询组织机构接口")
    @LogAnnotation(title = "机构新闻", action = "查询组织机构")
//    @RequiresPermissions("sysOrganMessage:detail")
    @ResponseBody
    public DataResult<SysContentEntity> detailInfo(@PathVariable("type") String type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type", type);
        queryWrapper.eq("dept_id", deptService.rootDept());
        SysOrganMessageEntity organMessageEntity = sysOrganMessageService.getOne(queryWrapper);
        if (null == organMessageEntity) {
            return DataResult.success();
        }
        return DataResult.success(sysContentService.getById(organMessageEntity.getContentId()));
    }
}
